﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ListItem.cs" company="SMART business">
//   Copyright (c) 2014 All Rights Reserved   
// </copyright>
// <summary>
//   The list view item.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ComparisonTool
{
    using System.Collections;
    using System.Windows.Forms;

    /// <summary>
    /// The list view item.
    /// </summary>
    public class ListItem : IComparer
    {
        /// <summary>
        /// The column.
        /// </summary>
        private readonly int column;

        /// <summary>
        /// The order.
        /// </summary>
        private readonly SortOrder order;

        /// <summary>
        /// Initializes a new instance of the <see cref="ListItem"/> class.
        /// </summary>
        /// <param name="column">
        /// The column.
        /// </param>
        /// <param name="order">
        /// The order.
        /// </param>
        public ListItem(int column, SortOrder order)
        {
            this.column = column;
            this.order = order;
        }

        /// <summary>
        /// The compare.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int Compare(object x, object y)
        {
            if (this.order == SortOrder.Descending)
            {
                return 1;
            }

            if (((ListViewItem)x).SubItems.Count <= this.column || ((ListViewItem)y).SubItems.Count <= this.column)
            {
                return 1;
            }

            return string.CompareOrdinal(((ListViewItem)x).SubItems[this.column].Text, ((ListViewItem)y).SubItems[this.column].Text);
        }
    }
}
